package com.smasher.serialport;


import android.content.Context;
import android.util.Log;

public class SerialPortProxy implements SerialPortImpl {

    public static final String TAG = "Proxy";
    private SerialPortImpl real;

    private SerialPortProxy() {

    }

    private static class SingletonInstance {
        private static final SerialPortProxy INSTANCE = new SerialPortProxy();
    }

    public static SerialPortProxy getInstance() {
        return SingletonInstance.INSTANCE;
    }


    public void setReal(SerialPortImpl impl) {
        this.real = impl;
    }

    /**
     * 初始化 rtu
     *
     * @param path     path
     * @param baud     baud
     * @param databits databits
     * @param parity   NONE ODD EVEN MARK SPACE
     * @param stopbits stopbits
     */
    @Override
    public void init(String path, int baud, int databits, String parity, int stopbits) {
        if (real != null) {
            real.init(path, baud, databits, parity, stopbits);
        }
    }

    @Override
    public void open() {
        if (real != null) {
            real.open();
        }
    }

    @Override
    public void close() {
        if (real != null) {
            Log.d(TAG, "close: ");
            real.close();
        }
    }

    @Override
    public void write(byte[] sendData) {
        if (real != null) {
            real.write(sendData);
        }
    }

    @Override
    public void read(byte[] sendData) {
        if (real != null) {
            real.read(sendData);
        }
    }

    @Override
    public void addDataReceiveListener(OnDataReceiveListener listener) {
        if (real != null) {
            real.addDataReceiveListener(listener);
        }
    }

    @Override
    public String[] getAllDevicesPath(Context context) {
        if (real != null) {
            return real.getAllDevicesPath(context);
        }
        return new String[0];
    }

    @Override
    public void setDebug(boolean isDebug) {
        if (real != null) {
            real.setDebug(isDebug);
        }
    }

}
